var express = require('express');
var router = express.Router();
var stuModel = require('../database/model/stuModel.js')

/* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });

router.get('/demo', function(req, res, next) {
  res.send('使用生成器快速搭建express项目');
});

router.get('/', function(req, res, next) { //查询
  // console.log('stu接口',req.headers);
  stuModel.find().then(result=>{
    // console.log(result);
    res.send({
      code:1,
      msg:'学员列表获取成功',
      data:result
    })
  })
});

router.post('/', function(req, res, next) { //新增
  console.log(req.body);
  new stuModel({
    name:req.body.name,
    score:req.body.score,
    gender:req.body.gender
  }).save().then(result=>{
    // console.log(result);
    res.send({
      code:1,
      msg:'录入成功'
    })
  })
});

router.put('/', function(req, res, next) { //修改
  // console.log(req.body);
  let {_id} = req.body
  stuModel.update({_id},req.body).then(result=>{
    console.log(result);
    res.send({
      code:1,
      msg:'修改成功'
    })
  })
});


module.exports = router;
